﻿<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header.php"); ?>

<?php 
// nu esli get imeet "confirm" nado podtverzhdat' vseh v migracii u kogo data value date = confirm 
if(isset($_GET['confirm'])){
	$sql = "UPDATE `migracija` SET `partvirtinta` = 1 WHERE `kelimodata` = '";
	$sql .= ($_GET['confirm']);
	$sql .="'; ";
	$confirm = mysql_query($sql, $connection);
	confirm_query($confirm);
	
};
?>


<?php //postcheck main part top form forming
 $postintas =0;
 $svarstdata=0;
 if (isset($_POST['svarstymodata'])){
 $svarstdata = $_POST['svarstymodata'];
 //echo "1";
 };
	if (isset($_POST['submit']) or (isset($_POST['svarstymodata']))) {
		$postintas =1;
		//echo "2";
		if($svarstdata ==0){
			$svarstdata=$_POST['svarstymodata'];
		};
		//Darbotvarkes count.
		$count2 = 0;
		$darbotvarke = "<p style=\"text-align: left;\">Darbotvarkė </p>";
		$darbotvarke .= "<ol>";	
	};
		?>
		
<?php //o dar kazkaip reikia header ir footer padaryti....
if ($postintas==1){	


	$query = "SELECT * FROM `skirstimassudetis` WHERE svarstdata='";
	$query .=$svarstdata;
	$query .="'";
	$query .=" LIMIT 1";
	$test_set = mysql_query($query, $connection);
	confirm_query($test_set);
	$test_count = mysql_affected_rows($connection);
	//echo $test_count;
	if ($test_count ==0){
	//FIX
	
		$query = "SELECT * FROM `skirstimassudetis` WHERE ";
		$query .=" `tipas` = 1";
		$query .=" ORDER BY `svarstdata` DESC ";
		$query .=" LIMIT 1";
		$findlatest_set = mysql_query($query, $connection);
		confirm_query($findlatest_set);
		$findlatest = mysql_fetch_array($findlatest_set);
		//echo $findlatest['svarstdata'];
		
		$query = "SELECT * FROM `skirstimassudetis` WHERE svarstdata='";
		$query .=($findlatest['svarstdata']);
		$query .="'";
		$fix_set = mysql_query($query, $connection);
		confirm_query($fix_set);
		$test_count = mysql_affected_rows($connection);
		while ($fix = mysql_fetch_assoc($fix_set)){
		//echo "boo";
		//echo $c++;
		//insert some shit
		$query = "INSERT INTO `skirstimassudetis` (`svarstdata` ,`dalyvisid` ,`tipas`) VALUES (";
		$query .= "'" ;
		$query .=  $svarstdata;
		$query .= "'," ;
		$query .= ($fix['dalyvisid']);
		$query .=  ",";
		$query .= ($fix['tipas']);
		$query .= ");" ;
		$insert = mysql_query($query, $connection);
		confirm_query($insert);
		}; 
	};

	//Pirmininkas
	
	$query = "SELECT * FROM `skirstimassudetis`,`personalas` WHERE svarstdata='";
	$query .=$svarstdata;
	$query .="'";
	$query .=" AND `tipas` = 1";
	$query .=" AND `skirstimassudetis`.`dalyvisid` = `personalas`.`personalasid` ";
	$query .=" LIMIT 1";
	$pirmininkas_set = mysql_query($query, $connection);
	$pirmininkas_count = mysql_affected_rows($connection);
	//echo $pirmininkas_count;
	if ($pirmininkas_count ==1){
	
		confirm_query($pirmininkas_set);
		$pirmininkas = mysql_fetch_array($pirmininkas_set);
	}else{
		$query = "SELECT * FROM `skirstimassudetis`,`personalas` WHERE ";
		$query .=" `tipas` = 1";
		
		$query .=" AND `skirstimassudetis`.`dalyvisid` = `personalas`.`personalasid` ";
		$query .=" ORDER BY `svarstdata` DESC ";
		$query .=" LIMIT 1";
		$pirmininkas_set = mysql_query($query, $connection);
		confirm_query($pirmininkas_set);
		$pirmininkas = mysql_fetch_array($pirmininkas_set);
	};
	
	
	//sekretorius
	$query = "SELECT * FROM `skirstimassudetis`,`personalas` WHERE svarstdata='";
	$query .=$svarstdata;
	$query .="'";
	$query .=" AND `tipas` = 2";
	$query .=" AND `skirstimassudetis`.`dalyvisid` = `personalas`.`personalasid` ";
	$query .=" LIMIT 1";
	$sekretorius_set = mysql_query($query, $connection);
	$sekretorius_count = mysql_affected_rows($connection);
	//echo $sekretorius_count;
	if ($sekretorius_count ==1){
		confirm_query($sekretorius_set);
		$sekretorius = mysql_fetch_array($sekretorius_set);
		
	}else{
		$query = "SELECT * FROM `skirstimassudetis`,`personalas` WHERE ";
		$query .=" `tipas` = 2";
		$query .=" AND `skirstimassudetis`.`dalyvisid` = `personalas`.`personalasid` ";
		$query .=" ORDER BY `svarstdata` DESC ";
		$query .=" LIMIT 1";
		$sekretorius_set = mysql_query($query, $connection);
		confirm_query($sekretorius_set);
		$sekretorius = mysql_fetch_array($sekretorius_set);
		
	};

	$query = "SELECT * 
	FROM `personalas`, `skirstimassudetis`
	WHERE `skirstimassudetis`.`svarstdata` ='";
	$query .= $svarstdata;
	$query .= "' AND `skirstimassudetis`.`dalyvisid`=";
	$query .= "`personalas`.`personalasid`";
	$query .= " AND `skirstimassudetis`.`tipas`= 3";
	$query .= " ORDER BY personalasid ASC";
	$personalas_set = mysql_query($query, $connection);
	confirm_query($personalas_set);
	
	

	$header = "";
	$header .= "<strong><p style=\"text-align: center;\">";
	$header .= "PRAVIENIŠKIŲ PATAISOS NAMAI-ATVIROJI KOLONIJA";
	$header .= "<BR>";
	//$header .= "2-oji VALDYBA";
	$header .= "<BR>";
	$header .= "<BR>";
	$header .= "NUTEISTŲJŲ SKYRIMO Į BŪRIUS KOMISIJOS POSĖDŽIO";
	$header .= "<BR>";
	$header .= "PROTOKOLAS";
	$header .= "</strong>";
	$header .= "<BR>";
	$header .= "<BR>";
	$header .= $svarstdata;
	$header .= " Nr.";
	$header .= "<BR>";
	$header .= "Pravieniškės II";
	$header .= "</p>";
	$header .= "<BR>";
	$header .= "Posėdžio pirmininkas ";
	$header .= "<a href=\"skirstimassudetis.php?data=";
	$header .= $svarstdata;
	$header .= "\">";
	$header .= ($pirmininkas["vardas"])." ".($pirmininkas["pavarde"]);
	$header .= "</a>";
	$header .= "<br>";
	$header .= "Posėdžio sekretorius ";
	$header .= "<a href=\"skirstimassudetis.php?data=";
	$header .= $svarstdata;
	$header .= "\">";
	$header .= ($sekretorius["vardas"])." ".($sekretorius["pavarde"]);
	$header .= "</a>";
	$header .= "<br>";
	$header .= "Dalyvauja ";
	
	while ($personalas = mysql_fetch_array($personalas_set)) {
		$header .= "<a href=\"skirstimassudetis.php?data=";
		$header .= $svarstdata;
		$header .= "\">";
		$header .= $personalas["vardas"] ." ".$personalas["pavarde"];
		$header .= "</a>";
		$header .= ", ";
	};
		
	$header .= "<br>";


	$footer = "";
	$footer .= "<br><table width=\"100%\" border=\"0\" cellspacing=\"2\" cellpadding=\"0\"><tr>";
	$footer .= "<td align=\"left\">";
	$footer .= "<p>Posėdžio pirmininkas</p>";
	$footer .= " </td><td align=\"right\"><p>";
	$footer .= ($pirmininkas["vardas"])." ".($pirmininkas["pavarde"]);
	$footer .= "</p></td></tr><tr><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td align=\"left\">";
	$footer .= "<p>Posėdžio sekretorius</p>";
	$footer .= "</td><td align=\"right\"><p>";
	$footer .= ($sekretorius["vardas"])." ".($sekretorius["pavarde"]);
	$footer .= "</p></td></tr><tr><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td align=\"left\">";
	$footer .= "<p>Nariai:</p>";
	$footer .= "</td><td>&nbsp;</td></tr></table>";
	$footer .= "";

  

};

?>		
		

<?php //nauji
	if ($postintas==1){		
		$sql = "SELECT * ";
		$sql .= " FROM `migracija`, `user`, `lovos`";
		$sql .= " WHERE `migracija`.`kelimodata` = ";
		$sql .= "'".$svarstdata."'";
		$sql .= " AND `migracija`.`userid` = ";
		$sql .= "`user`.`userid`";
		$sql .= " AND `user`.`lovosid` = ";
		$sql .= "`lovos`.`lovosid`";
		$sql .= " AND `migracija`.`fromlovosid` = ";
		$sql .= "0";
		
		
		$sql .= " ORDER BY `migracija`.`migracijaid` ASC";
		
		//echo " ".$sql." ";
		
		$result = mysql_query($sql, $connection);
		$result_count = 0;
		$result_count = mysql_affected_rows($connection);
		//echo $result_count;
		if($result){
			//echo "resut";
			if($result_count!=0){
				//Skaitliukas1
				$nauji_yra=1;
				$count1=1;
				$count2++;
				$darbotvarke.="<li>Naujai atvykusių skyrimas į būrius.</li>";
				
				$nauji_ready .= "<p style=\"text-align: justify; padding-left: 20px;\">".$count2.". SVARSTYTA. Naujai atvykusių skyrimas į būrių brigadas. <p>";
				
				do { 
					if(($row_result['bylosnr']) != NULL)	{
						$nauji_ready .= "<a href=\"edit_burys.php?userid=";
						$nauji_ready .= $row_result['userid'];
						$nauji_ready .= "&migracijaid=";
						$nauji_ready .= $row_result['migracijaid'];
						$nauji_ready .= "\">";
						
						$nauji_ready .= "<p style=\"text-align: justify; padding-left: 20px;\">".$count2.".".$count1." ";
						$count1++;
						//BylosNr
						$nauji_ready .= $row_result['bylosnr']." ";
						//pavarde
						$nauji_ready .= $row_result['pavarde'] ." ";
						//vardas
						$nauji_ready .= $row_result['vardas']." ";
						$nauji_ready .= "<br>";
						//Nutarta
						$nauji_ready .= "NUTARTA:<br>";
						
												
						
						$nauji_ready .="Skirti į ";
						
						$sql = "SELECT * ";
						$sql .= "FROM `lovos`";
						$sql .= "WHERE `lovos`.`lovosid` = ";
						$sql .= $row_result['tolovosid'];
						$lova_set = mysql_query($sql, $connection);
						$tolova = mysql_fetch_assoc($lova_set);
						
						
						$nauji_ready .= $tolova['burys'];
						$nauji_ready .=" būrio ";
						$nauji_ready .=$tolova['brigada'];
						$nauji_ready .=" brigadą ";
						$nauji_ready .=$tolova['lova'];
						$nauji_ready .=" lovą.";
						
						$nauji_ready .="</a>";
						$nauji_ready .="<p>";
						//$isvyka_ready .= $row_result['nutarta']." <br>";
					};			
				} while ($row_result = mysql_fetch_assoc($result)); 
			}else{
				$nauji_yra=0;
			};	
		} else {
			// Display error message.
			$message .= "<p>Client update failed.</p>";
			$message .= "<p>" . mysql_error() . "</p>";
		};
	};
?>

<?php //seni
	if ($postintas==1){		
		$sql = "SELECT * ";
		$sql .= "FROM `migracija`, `user`, `lovos`";
		$sql .= "WHERE `migracija`.`kelimodata` = ";
		$sql .= "'".$svarstdata."'";
		$sql .= "AND `migracija`.`userid` = ";
		$sql .= "`user`.`userid`";
		$sql .= "AND `user`.`lovosid` = ";
		$sql .= "`lovos`.`lovosid`";
		$sql .= "AND `migracija`.`fromlovosid` != ";
		$sql .= "0";
		
		
		$sql .= " ORDER BY `migracija`.`migracijaid` ASC";
		
		$result = mysql_query($sql, $connection);
		$result_count = 0;
		$result_count = mysql_affected_rows($connection);
		
		if($result){
			if($result_count!=0){
				//Skaitliukas1
				$seni_yra=1;
				$count1=1;
				$count2++;
				// $darbotvarke.="<li>Naujai atvykusių skyrimas į būrius.</li>";
				$darbotvarke.="<li>Pervedimai iš būrio į būrį.</li>";
				// $seni_ready .= "<p style=\"text-align: justify; padding-left: 20px;\">".$count2.". SVARSTYTA. Naujai atvykusių skyrimas į būrių brigadas. <p>";
				$seni_ready .= "<p style=\"text-align: justify; padding-left: 20px;\">".$count2.". SVARSTYTA. Pervedimai iš būrio į būrį.<p>";
				do { 
					if(($row_result['bylosnr']) != NULL)	{
						$seni_ready .= "<a href=\"edit_burys.php?userid=";
						$seni_ready .= $row_result['userid'];
						$seni_ready .= "&migracijaid=";
						$seni_ready .= $row_result['migracijaid'];
						$seni_ready .= "\">";
						
						$seni_ready .= "<p style=\"text-align: justify; padding-left: 20px;\">".$count2.".".$count1." ";
						$count1++;
						//BylosNr
						$seni_ready .= $row_result['bylosnr']." ";
						//pavarde
						$seni_ready .= $row_result['pavarde'] ." ";
						//vardas
						$seni_ready .= $row_result['vardas']." ";
						$seni_ready .= "<br>";
						//Nutarta
						$seni_ready .= "NUTARTA:<br>";
						
						$sql = "SELECT * ";
						$sql .= "FROM `lovos`";
						$sql .= "WHERE `lovos`.`lovosid` = ";
						$sql .= $row_result['fromlovosid'];
						$lova_set = mysql_query($sql, $connection);
						$fromlova = mysql_fetch_assoc($lova_set);
						
						
						$seni_ready .="Pervesti iš ";
						$seni_ready .= $fromlova['burys'];
						$seni_ready .=" būrio ";
						$seni_ready .=$fromlova['brigada'];
						$seni_ready .=" brigados ";
						$seni_ready .=$fromlova['lova'];
						$seni_ready .=" lovos į ";
						
						$sql = "SELECT * ";
						$sql .= "FROM `lovos`";
						$sql .= "WHERE `lovos`.`lovosid` = ";
						$sql .= $row_result['tolovosid'];
						$lova_set = mysql_query($sql, $connection);
						$tolova = mysql_fetch_assoc($lova_set);
						
						
						$seni_ready .= $tolova['burys'];
						$seni_ready .=" būrio ";
						$seni_ready .=$tolova['brigada'];
						$seni_ready .=" brigadą ";
						$seni_ready .=$tolova['lova'];
						$seni_ready .=" lovą.";
						
						$seni_ready .="</a>";
						$seni_ready .="<p>";
						//$isvyka_ready .= $row_result['nutarta']." <br>";
					};			
				} while ($row_result = mysql_fetch_assoc($result)); 
			}else{
				$seni_yra=0;
			};	
		} else {
			// Display error message.
			$message .= "<p>Client update failed.</p>";
			$message .= "<p>" . mysql_error() . "</p>";
		};
	};
?>

<?php //Ordered List uzbaigimas
$darbotvarke .= "</ol>";
?>


			

					<article>
						<h2>Drausmės komisijos posėdžio protokolo formavimas</h2>
	<?php 
	
	echo $message."</br>";
	
	
	
	if($count2==0 and $postintas==1){
		echo "Oblomis' staraja, net na segodnia raboty =]";
	}else{
		//nado delat' knopku patvirtinti...
		echo $header;
		echo $darbotvarke;
	};
	
	if($nauji_yra==1){
		echo $nauji_ready;
	};
	if($seni_yra==1){
		echo $seni_ready;
	};
	
	if($count2==0 and $postintas==1){
	}else{
		
		echo $footer;
		
	};?>
	
<form action="form_prekelimai.php" method="POST" name="submit">
  <?php
	if($postintas==1 and $count2!=0){
		echo "<a href=\"form_prekelimai.php?confirm=";
		echo $svarstdata;
		echo "\">Patvirtinti</a>";
	};
	
	?>
  </br>
  Svarstymo data: <input name="svarstymodata" type="date" value="" placeholder="1900-12-31" required/>
 <br>
 
  
<input name="submit" type="submit" value="SubmiNt" />
</form>



				  </article>
			
            
<?php require("includes/footer.php"); ?>
